import userDao from "../Dao/user.js";
import {captchaTypes} from "../roleType/captchaTypes.js";
import {generateRandomString, httpResult, sendEmail_post, sendEmail_post_html} from "../utils/utils.js";
import {emailRegister_1, retrievePassword_1} from "../utils/templateUtils.js";
import captchaDao from "../Dao/captcha.js";
import sequelize from "../models/init.js";
import captchaNewDao from "../Dao/captchaNew.js";

// 找回密码第一步 发送邮件验证邮箱
export const sendEmailUpdatePassword = async (req, res) => {
  let {email, username} = req.body;
  if (!username && !email) {
    throw new Error('账户不能为空');
  }
  // 验证用户名或邮箱是否存在
  let user = await userDao.getUserByUsernameOrEmail(username, email);
  if (!user) {
    throw new Error('账户不存在');
  }
  let code = await createCaptcha(user.id, captchaTypes.EMAIL_VERIFY);
  let result = await sendEmail_post_html(user.email, retrievePassword_1(code));
  if(!result) {
    throw new Error('发送邮件失败');
  }
  res.json(httpResult.success({result: true}));
};
// 找回密码第二步 验证验证码 返回密码修改的唯一验证码
export const verifyCaptcha = async (req, res) => {
  let {code, username, email} = req.body;
  if (!username && !email) {
    throw new Error('账户不能为空');
  }
  if(!code) {
    throw new Error('验证码不能为空');
  }
  // 验证用户名或邮箱是否存在
  let user = await userDao.getUserByUsernameOrEmail(username, email);
  if (!user) {
    throw new Error('账户不存在');
  }
  let result = await captchaDao.verify(user.id, captchaTypes.EMAIL_VERIFY, code);
  if(!result) {
    throw new Error("验证码错误");
  }
  // 返回唯一码
  let code2 = await createCaptcha(user.id, captchaTypes.UPDATE_PASSWORD);
  res.json(httpResult.success({result: true, code: code2}));
};
// 找回密码第三步 验证唯一码 修改密码
export const updatePassword = async (req, res) => {
  let {code, password, username, email} = req.body;
  if (!username && !email) {
    throw new Error('账户不能为空');
  }
  if(!code) {
    throw new Error('验证码不能为空');
  }
  if(!password) {
    throw new Error('密码不能为空');
  }
  // 验证用户名或邮箱是否存在
  let user = await userDao.getUserByUsernameOrEmail(username, email);
  if (!user) {
    throw new Error('账户不存在');
  }
  // 验证唯一码
  let result = await captchaDao.verify(user.id, captchaTypes.UPDATE_PASSWORD, code);
  if(!result) {
    throw new Error("验证码错误");
  }
  let t = await sequelize.transaction();
  try {
    // 修改密码
    await userDao.updatePassword(user.id, password, t);
    // 删除唯一码
    await captchaDao.delete(user.id, captchaTypes.UPDATE_PASSWORD, t);
    await t.commit();
  }catch (e) {
    await t.rollback();
    throw new Error('更新密码失败');
  }
  res.json(httpResult.success({result: true}));
};
// 邮箱注册第一步 邮箱注册获取验证码
export const getEmailVerifyCode = async (req, res) => {
  let {email} = req.body;
  if (!email) {
    throw new Error('邮箱不能为空');
  }
  let code = await createCaptchaNew(email, captchaTypes.EMAIL_REGISTER);
  let result = await sendEmail_post_html(email, emailRegister_1(code));
  if(!result) {
    throw new Error('发送邮件失败');
  }
  res.json(httpResult.success({result: true}));
};

// 生成随机的6位验证码 并且该用户邀请码系统该类型验证码不一致,更新数据库该类型验证码
const createCaptcha = async (user_id, type) => {
  try {
    let code = generateRandomString(6).toUpperCase();
    // 检查数据库是否存在
    let result = await captchaDao.verify(user_id, type, code);
    // 如果有再生成一个
    if (result) {
      return await createCaptcha(user_id, type);
    }
    // 成功就更新数据库该类型验证码
    await captchaDao.updateOrCreate(user_id, type, code);
    return code;
  }catch (e) {
    throw new Error('发送验证码失败');
  }
};

// 生成随机的6位验证码 并且该用户邀请码系统该类型验证码不一致,更新数据库该类型验证码 用于新验证码数据库
const createCaptchaNew = async (email, type) => {
  try {
    let code = generateRandomString(6).toUpperCase();
    // 检查数据库是否存在
    let result = await captchaNewDao.verify(email, type, code);
    // 如果有再生成一个
    if (result) {
      return await createCaptchaNew(email, type);
    }
    // 成功就更新数据库该类型验证码
    await captchaNewDao.updateOrCreate(email, type, code);
    return code;
  }catch (e) {
    throw new Error('发送验证码失败');
  }
};

